package com.zch.action;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.base.dialect.PaginationSupport;
import com.base.dao.model.Ret;
import com.base.dao.model.Grid;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.apache.commons.lang.StringUtils;
import com.base.util.JSONUtils;
import com.base.web.annotation.LoginFilter;
import com.item.service.MessageService;

import org.springframework.beans.factory.annotation.Autowired;
import com.base.action.CoreController;
import com.zch.dao.model.Activitymessage;
import com.zch.dao.model.Collegemessage;
import com.zch.dao.model.CollegemessageExample;
import com.zch.dao.model.CompanyRegister;
import com.zch.dao.model.ProjectMessage;
import com.zch.dao.model.ZaitiType;
import com.zch.dao.model.ZxMessage;
import com.zch.daoEx.model.ActivitymessageEx;
import com.zch.daoEx.model.CollegemessageEx;
import com.zch.service.CollegemessageService;
import com.zch.service.ProjectMessageService;

@RequestMapping("collegemessage")
@Controller
@LoginFilter
public class CollegemessageController extends CoreController{

	@Autowired
	private CollegemessageService collegemessageService;
	@Autowired
    private MessageService messageService;

	@RequestMapping("/list")
	@ResponseBody
	public String list(Integer page, Integer rows, String name){
	    PaginationSupport.byPage(page, rows);
	    Map<String, Object> map = new HashMap<String, Object>();
	    map.put("name", name);
	    List<CollegemessageEx> list = collegemessageService.selectList(map);
	    return JSONUtils.serialize(new Grid(PaginationSupport.getContext().getTotalCount(), list));
	}

	@RequestMapping("/save")
	@ResponseBody
	public String save(Collegemessage collegemessage){
	    if (StringUtils.isBlank(collegemessage.getId())){
	        collegemessageService.insert(collegemessage);
        }else{
            int result = collegemessageService.updateByPrimaryKeySelective(collegemessage);
            if(result==1){
                collegemessage= collegemessageService.selectByPrimaryKey(collegemessage.getId());
                if(collegemessage.getIsAgree()==2){
                    String pushContent = "审核未通过!理由:"+collegemessage.getNotAgreeReason();
                    //推送
                    messageService.findMessage(collegemessage.getName(), pushContent, collegemessage.getUserId(), 1);
                }
            }
        }
			
		return JSONUtils.serialize(new Ret(0));
	}

	@RequestMapping("/findById")
	@ResponseBody
	public String find(String id){
		Collegemessage collegemessage = collegemessageService.selectByPrimaryKey(id);
		collegemessage.setContent(collegemessage.getContent().replaceAll("\r\n", "<br/>"));
		collegemessage.setTecresult(collegemessage.getTecresult().replaceAll("\r\n", "<br/>"));
		return JSONUtils.serialize(collegemessage);
	}
	
	@RequestMapping("/findByIds")
    @ResponseBody
    public String finds(String id){
        Collegemessage collegemessage = collegemessageService.selectByPrimaryKey(id);
        collegemessage.setContent(collegemessage.getContent().replaceAll("\r\n", "<br/>").replaceAll("\r", "<br/>").replaceAll("\n", "<br/>").replaceAll("\\s", "&nbsp;"));
        collegemessage.setTecresult(collegemessage.getTecresult().replaceAll("\r\n", "<br/>").replaceAll("\r", "<br/>").replaceAll("\n", "<br/>").replaceAll("\\s", "&nbsp;"));
        return JSONUtils.serialize(collegemessage);
    }

	@RequestMapping("/del")
	@ResponseBody
	public String del(String id){
		String[] ids = id.split(",");
		for (String str : ids){
			collegemessageService.deleteByPrimaryKey(str);
		}
		return JSONUtils.serialize(new Ret(0));
	}
	
    @RequestMapping("/update")
    @ResponseBody
    public String update(Collegemessage collegemessage){
        int result = collegemessageService.updateByPrimaryKeySelective(collegemessage);
        if(result == 1){
            if(collegemessage.getIsAgree()==1){
              String  pushContent = "审核通过";
                //推送
               collegemessage= collegemessageService.selectByPrimaryKey(collegemessage.getId());
                messageService.findMessage(collegemessage.getName(), pushContent, collegemessage.getUserId(), 1);
            }
        }
        return JSONUtils.serialize(new Ret(0));
    }
    
    @RequestMapping("/myList")
    @ResponseBody
    public String myList(Integer page, Integer rows, String name){
        PaginationSupport.byPage(page, rows);
        String id = getSessionParameter("id");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", name);
        map.put("id", id);
        List<CollegemessageEx> list = collegemessageService.selectMyList(map);
        return JSONUtils.serialize(new Grid(PaginationSupport.getContext().getTotalCount(), list));
    }

    @RequestMapping("/myListSave")
    @ResponseBody
    public String myListSave(Collegemessage collegemessage){
        collegemessage.setUserId(getSessionParameter("id"));
        if(!getSessionParameter("sys").equals("inside")){
            return JSONUtils.serialize(new Ret(-1,"请重新登录..."));
        }
        if (StringUtils.isBlank(collegemessage.getId())){
            collegemessageService.insert(collegemessage);
        }else{
            collegemessageService.updateByPrimaryKeySelective(collegemessage);
        }
        return JSONUtils.serialize(new Ret(0));
    }
    
    @RequestMapping("/top")
    @ResponseBody
    public String top(Collegemessage collegemessage){
        if (StringUtils.isBlank(collegemessage.getId())){
            collegemessage.setPublishTime(new Date());
            collegemessageService.insert(collegemessage);
        }else{
            collegemessage.setPublishTime(new Date());
            collegemessageService.updateByPrimaryKeySelective(collegemessage);
        }
        return JSONUtils.serialize(new Ret(0));
    }
}